﻿using System;
using System.IO;
using System.Runtime.Serialization;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using SymulatorEthernetWPF.Shared;
using SymulatorEthernetWPF.UserInterface;
using SymulatorEthernetWPF.SimObjects.Layers.Physical;
using SymulatorEthernetWPF.SimObjects.Layers.DataLink;
using System.Windows;
using System.Windows.Threading;

namespace SymulatorEthernetWPF.SimObjects.Devices
{
    [Serializable]
    internal class HostControl : DeviceControl
    {
        public HostControl()
        {

            icon.Source = StaticVars.pcISource;
            short interfaceNumber = 0;
            foreach (NetworkInterface it in interfaces)
            {
                it.Adress = new MacAdress("00:00:00:01:" + Simulation.HostCounter.ToString("X2") + ":" + interfaceNumber.ToString("X2"));
                interfaceNumber++;
            }
            deviceLabel.Content = "HOST " + Simulation.HostCounter;
            using (var stream = new MemoryStream())
            {
                var encoder = new PngBitmapEncoder { Interlace = PngInterlaceOption.On };
                if (icon.Source != null) encoder.Frames.Add(BitmapFrame.Create((BitmapSource)icon.Source));
                encoder.Save(stream);
                RawImageData = stream.ToArray();
            }
            Simulation.HostCounter++;

        }

        public HostControl(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
        }

        internal override void startReceiving(Signal signal, NetworkInterface networkInterface)
        {
            if (signal.isJam)
            {
                for (int i = networkInterface.wireEnd.signals.Count - 1; i >= 0; i--)
                {
                    networkInterface.wireEnd.signals[i].stop();
                }                
            }
        }        

        internal override void stopReceiving(Signal signal, NetworkInterface networkInterface)
        {
            if (!signal.isJam)
            {
                if (signal.ethernet.destination.Equals(networkInterface.Adress) || signal.ethernet.destination.Equals(new MacAdress("FF:FF:FF:FF:FF:FF")))
                {
                    receivedData.Add(new ReceivedData(signal.ethernet, Simulation.tickCount, FrameAction.Received));
                }
                else
                {
                    receivedData.Add(new ReceivedData(signal.ethernet, Simulation.tickCount, FrameAction.Rejected));
                }
            }

        }
        internal override void abortReceiving(Signal signal, NetworkInterface networkInterface)
        {
            return;
        }
        internal override void reset()
        {
            return;
        }

        public override void sendFrame(Ethernet frameData)
        {
            NetworkInterface senderInterface = null;            
            foreach (NetworkInterface it in interfaces)
            {
                if (it.Adress.Equals(frameData.source))
                {
                    senderInterface = it;
                    break;
                }
            }
            if (senderInterface != null && senderInterface.wireEnd != null)
            {
                frameData.source = senderInterface.Adress;
                senderInterface.sendFrame(frameData);
            }
        }

        protected override void DeviceControlContextMenuPropertiesClick(object sender, RoutedEventArgs e)
        {
            (new HostSettings(this)).ShowDialog();
        }
    }
}